﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Telerik.Web.UI;

namespace BVVD_Website.userRight
{
    public partial class frmGroup_Menu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();
                var listNSD = (from nsd in db.LST_NSDs
                               select nsd).ToList().OrderBy(a => a.TENNSD);
                ddlNSD.DataSource = listNSD;
                ddlNSD.DataValueField = "MANSD";
                ddlNSD.DataTextField = "TENNSD";
                ddlNSD.DataBind();
                fillgrvNSD_MENU();
                grvNSD_MENU.DataBind();

            }
        }

        protected void ddlNSD_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            fillgrvNSD_MENU();
            grvNSD_MENU.DataBind();
        }

        private void fillgrvNSD_MENU()
        {
            BVVDDataContext db = new BVVDDataContext();

            var list = (from mn in db.LST_MENUs
                        where mn.TYPE == 2 && mn.LEVEL == 2
                        join nsd_mn in db.F_NSD_MENUs on mn.MAMN equals nsd_mn.MAMN
                        into temp1
                        from tmp1 in temp1.DefaultIfEmpty()
                        select new
                        {
                            RECID = mn.RECID,
                            MAMN = mn.MAMN,
                            TENMN = mn.TEN,
                            MANSD = tmp1.MANSD == null ? "" : tmp1.MANSD,
                            SHOW = tmp1.SHOW != null ? (tmp1.SHOW.ToString().Equals("1") ? 1 : 0) : 0,
                            ADD = tmp1.ADD != null ? (tmp1.ADD.ToString().Equals("1") ? 1 : 0) : 0,
                            EDIT = tmp1.EDIT != null ? (tmp1.EDIT.ToString().Equals("1") ? 1 : 0) : 0,
                            DELETE = tmp1.DELETE != null ? (tmp1.DELETE.ToString().Equals("1") ? 1 : 0) : 0
                        }).ToList();

            if (ddlNSD.SelectedIndex >= 0)
            {
                if (list.Where(a => a.MANSD.Equals(ddlNSD.SelectedValue)).Count() > 0)
                {
                    grvNSD_MENU.DataSource = list.Where(a => a.MANSD.Equals(ddlNSD.SelectedValue));
                }
                else
                {
                    grvNSD_MENU.DataSource = list;
                }
            }
            else
            {
                grvNSD_MENU.DataSource = list.Where(a => a.RECID.Equals("N/A")); 

            }

        }

        protected void grvNSD_MENU_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillgrvNSD_MENU();
            //grvNSD_MENU.DataBind();
        }

        protected void grvNSD_MENU_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.Equals("SavePQ"))
            {
                BVVDDataContext db = new BVVDDataContext();
                foreach (GridDataItem dr in grvNSD_MENU.Items)
                {
                    string mamn = dr["MAMN"].Text;
                    string mansd = ddlNSD.SelectedValue;
                    int show = (dr.FindControl("chbShow") as CheckBox).Checked ? 1 : 0;
                    int add = (dr.FindControl("chbAdd") as CheckBox).Checked ? 1 : 0;
                    int edit = (dr.FindControl("chbEdit") as CheckBox).Checked ? 1 : 0;
                    int delete = (dr.FindControl("chbDelete") as CheckBox).Checked ? 1 : 0;

                    F_NSD_MENU obj = new F_NSD_MENU
                    {
                        MANSD = mansd,
                        MAMN = mamn,
                        SHOW = show,
                        ADD = add,
                        EDIT = edit,                        
                        DELETE = delete,
                        NGAYGAN = DateTime.Now,
                        NGUOIGAN = Session["user"] != null ? (Session["user"] as LST_NSD).MANSD : ""
                    };
                    checkRowExist(obj);


                }
                grvNSD_MENU.Rebind();
                rnAlert.Text = "Lưu thông tin thành công ";
                rnAlert.Show();
            }
        }

        private string checkRowExist(F_NSD_MENU fc)
        {
            BVVDDataContext db = new BVVDDataContext();
            F_NSD_MENU value = (from ck in db.F_NSD_MENUs
                                 where ck.MAMN.Equals(fc.MAMN) && ck.MANSD.Equals(fc.MANSD)
                                 select ck).SingleOrDefault();
            if (value == null)
            {
                db.F_NSD_MENUs.InsertOnSubmit(fc);
                db.SubmitChanges();
                return "Add";
            }
            else if (!value.SHOW.ToString().Equals(fc.SHOW.ToString()) || !value.ADD.ToString().Equals(fc.ADD.ToString())
                || !value.EDIT.ToString().Equals(fc.EDIT.ToString()) || !value.DELETE.ToString().Equals(fc.DELETE.ToString())
                )
            {
                value.ADD = fc.ADD;
                value.SHOW = fc.SHOW;
                value.EDIT = fc.EDIT;
                value.DELETE = fc.DELETE;
                value.NGUOIGAN = Session["user"] != null ? (Session["user"] as LST_NSD).MANSD : fc.NGUOIGAN;
                value.NGAYGAN = DateTime.Now;
                db.SubmitChanges();
                return "Update";
            }
            else
            {

                return "Cancel";
            }

        }
    }
}